package com.ruoyi.pos.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.pos.domain.Goods;
import com.ruoyi.pos.service.IGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pos.domain.OrderItem;
import com.ruoyi.pos.service.IOrderItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * itemController
 * 
 * @author wzh
 * @date 2021-04-19
 */
@RestController
@RequestMapping("/dev-api/system/pos/item")
public class OrderItemController extends BaseController
{
    @Autowired
    private IOrderItemService orderItemService;

    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询item列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderItem orderItem)
    {
        startPage();
        List<OrderItem> list = orderItemService.selectOrderItemList(orderItem);
        return getDataTable(list);
    }

    /**
     * 导出item列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:item:export')")
    @Log(title = "item", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OrderItem orderItem)
    {
        List<OrderItem> list = orderItemService.selectOrderItemList(orderItem);
        ExcelUtil<OrderItem> util = new ExcelUtil<OrderItem>(OrderItem.class);
        return util.exportExcel(list, "item");
    }

    /**
     * 获取item详细信息
     */
//    @PreAuthorize("@ss.hasPermi('pos:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(orderItemService.selectOrderItemById(id));
    }

    /**
     * 新增item
     */
//    @PreAuthorize("@ss.hasPermi('pos:item:add')")
    @Log(title = "item", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderItem orderItem)
    {
        Goods goods=goodsService.selectGoodsById(orderItem.getGid());



        //todo 可能之后迭代要增加优惠活动
        orderItem.setSalePrice(goods.getSalePrice());
        orderItem.setSubTotal(orderItem.getSalePrice() .multiply(new BigDecimal(orderItem.getNumber())) );
        orderItemService.insertOrderItem(orderItem);
        return toAjax(orderItemService.updateOrder(orderItem.getId()));
    }

    /**
     * 修改item
     */
//    @PreAuthorize("@ss.hasPermi('pos:item:edit')")
    @Log(title = "item", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderItem orderItem)
    {
        return toAjax(orderItemService.updateOrderItem(orderItem));
    }

    /**
     * 删除item
     */
//    @PreAuthorize("@ss.hasPermi('pos:item:remove')")
    @Log(title = "item", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderItemService.deleteOrderItemByIds(ids));
    }
}
